package com.nashuju.web.controller.role;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.durcframework.core.MessageResult;
import org.durcframework.core.controller.CrudController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nashuju.web.entity.role.Role;
import com.nashuju.web.entity.role.RoleSch;
import com.nashuju.web.entity.role.UserRole;
import com.nashuju.web.entity.role.SetRoleParam;
import com.nashuju.web.service.role.RolePermissionService;
import com.nashuju.web.service.role.RoleService;
import com.nashuju.web.service.role.UserRoleService;

@Controller
public class RoleController extends CrudController<Role, RoleService> {

    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private RolePermissionService rolePermissionService;

    @RequestMapping("/addRRole.do")
    public @ResponseBody MessageResult addRRole(SetRoleParam param) {
        if (StringUtils.hasText(param.getRoleName())) {
            Role role = new Role();
            role.setRoleName(param.getRoleName());
            this.getService().addRole(role, param.getSfId());
            return success();
        }
        return error("添加失败");
    }

    @RequestMapping("/listRolePermissionByRoleId.do")
    public @ResponseBody Object listRolePermissionByRoleId(int roleId) {
        return rolePermissionService.getRolePermissionByRole(roleId);
    }

    @RequestMapping("/listRRole.do")
    public @ResponseBody Object listRRole(RoleSch searchEntity) {
        return this.query(searchEntity);
    }

    @RequestMapping("/updateRRole.do")
    public @ResponseBody Object updateRRole(SetRoleParam param) {
        if (StringUtils.hasText(param.getRoleName())) {
            Role role = new Role();
            role.setRoleId(param.getRoleId());
            role.setRoleName(param.getRoleName());
            this.getService().updateRole(role, param.getSfId());
            return success();
        }
        return error("修改失败");
    }

    @RequestMapping("/delRRole.do")
    public @ResponseBody Object delRRole(Role enity) {
        return this.delete(enity);
    }

    @RequestMapping("/listRoleRelationInfo.do")
    public @ResponseBody Object listRoleRelationInfo(int roleId) {

        List<UserRole> userRoles = userRoleService.getUserRoleByRoleId(roleId);
        Map<String, Object> retMap = new HashMap<String, Object>();

        retMap.put("userRoles", userRoles);
        retMap.put("success", true);

        return retMap;
    }

    // ------------
    @RequestMapping("/listAllRRole.do")
    public @ResponseBody Object listAllRRole(RoleSch searchEntity) {
        return this.queryAll(searchEntity);
    }

}
